package com.twenty.seven.job.controller;

import com.twenty.seven.job.service.IJobService;
import com.twenty.seven.job.util.IPV4Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 注册定时，，cron的表达式，自己再百度
 * @Author twenty-seven
 * @Date 2020/1/13 9:52
 * @TODO
 **/
@Component
public class WorkTask {

    @Autowired
    private IJobService jobService;

    /**
     * 定时服务的名称
     */
    private static String serviceName = "service-job";

    /**
     * 每隔5s执行一次
     */
    @Scheduled(fixedDelay = 5000 )
    public void doWork1(){
        if (!IPV4Util.ipCompare(this.jobService.serviceUrl(serviceName))) {
            return;
        }
        try{
            //TODO do something
            System.out.println("现在时间："+new Date());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 每天10:30执行一次
     */
    @Scheduled(cron = "0 30 10 * * ?")
    public void doWork(){
        if (!IPV4Util.ipCompare(this.jobService.serviceUrl(serviceName))) {
            return;
        }
        try{
            //TODO do something
            System.out.println("每天10:30执行一次："+new Date());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
